﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.IotInfo;
using LJX.SmartPark.Service.IotInfo.Dto;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.IotInfo
{
    public class TricolourLightServeice : ITricolourLightServeice
    {
        private readonly IRepository<TricolourLight> _studentService;
        private readonly IRepository<TricolourLightLog> _studentService1;

        public TricolourLightServeice(IRepository<TricolourLight> studentService, IRepository<TricolourLightLog> studentService1)
        {
            _studentService = studentService;
            _studentService1 = studentService1;
        }

        /// <summary>
        /// 状态修改关闭
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> UpdateSelectTricol(TricolourLight input)
        {
            TricolourLight tricolourLight = input;
            tricolourLight.OpenStatus = false;
            tricolourLight.ClosingTime = DateTime.Now;

            

            ////记录日志
            //TricolourLightLog lightLog = new TricolourLightLog
            //{
            //    EquipMentNum = input.EquipMentNum,
            //    Principal = input.Principal,
            //    ConnectionStatus = input.ConnectionStatus,
            //    OpenStatus = input.OpenStatus,
            //    OpeningTime = input.OpeningTime,
            //    ClosingTime = input.ClosingTime,
            //    Remark = input.Remark,
            //};

            //var result = _studentService.UpdateAsync(input.MapTo<TricolourLight>());

            //_studentService1.InsertAsyncById(lightLog);

            
            return PutUpdateTricolourLight(tricolourLight);
        }

        /// <summary>
        /// 根据实体Id删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteByIdAsync(TricolourLight input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<TricolourLight>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _studentService.GetCount();
        }

        /// <summary>
        /// 显示分页查询
        /// </summary>
        /// <param name="fenye"></param>
        /// <returns></returns>
        public async Task<Response<List<TricolourLight>>> GetTricolourLightAsync(fenye fenye)
        {
            var students = await _studentService.GetAllListAsync();
            var result = students.MapToList<TricolourLight>();

            if (!string.IsNullOrWhiteSpace(fenye.equipMentNum))
            {
                result = result.Where(x => x.EquipMentNum.Contains(fenye.equipMentNum)).ToList();
            }

            if (!string.IsNullOrWhiteSpace(fenye.connectionStatus))
            {
                result = result.Where(x => x.ConnectionStatus == bool.Parse(fenye.connectionStatus)).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip(((fenye.pageIndex - 1) * fenye.pageSize)).Take(fenye.pageSize).ToList();

            return Response<List<TricolourLight>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<TricolourLight> GetTricolourLightById(TricolourLight input)
        {
            var student = _studentService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = student.MapTo<TricolourLight>();
            return Response<TricolourLight>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddTricolourLight(TricolourLight input)
        {
            var result = await _studentService.InsertAsyncById(input.MapTo<TricolourLight>());


            //记录日志
            TricolourLightLog lightLog = new TricolourLightLog
            {
                EquipMentNum = input.EquipMentNum,
                Principal = input.Principal,
                ConnectionStatus = input.ConnectionStatus,
                OpenStatus = input.OpenStatus,
                OpeningTime = input.OpeningTime,
                ClosingTime = input.ClosingTime,
                Remark = input.Remark,
            };

            _studentService1.InsertAsyncById(lightLog);

            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdateTricolourLight(TricolourLight input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<TricolourLight>());

            //记录日志
            TricolourLightLog lightLog = new TricolourLightLog
            {
                EquipMentNum = input.EquipMentNum,
                Principal = input.Principal,
                ConnectionStatus = input.ConnectionStatus,
                OpenStatus = input.OpenStatus,
                OpeningTime = input.OpeningTime,
                ClosingTime = input.ClosingTime,
                Remark = input.Remark,
            };

            _studentService1.InsertAsyncById(lightLog);


            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 状态修改启动
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> UpdateSelectTricol1(TricolourLight input)
        {
            TricolourLight tricolourLight = input;
            tricolourLight.OpenStatus = true;
            tricolourLight.OpeningTime = DateTime.Now;

            ////记录日志
            //TricolourLightLog lightLog = new TricolourLightLog
            //{
            //    EquipMentNum = input.EquipMentNum,
            //    Principal = input.Principal,
            //    ConnectionStatus = input.ConnectionStatus,
            //    OpenStatus = input.OpenStatus,
            //    OpeningTime = input.OpeningTime,
            //    ClosingTime = input.ClosingTime,
            //    Remark = input.Remark,
            //};

            //var result = _studentService.UpdateAsync(input.MapTo<TricolourLight>());

            //_studentService1.InsertAsyncById(lightLog);

            return PutUpdateTricolourLight(tricolourLight);
        }
    }

    /// <summary>
    /// 三色灯分页查询
    /// </summary>
    public class fenye
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
        public string? connectionStatus { get; set; }
        public string? equipMentNum { get; set; }
    }
}
